package com.hzh.jetpackcomposecompositionlocal.sample

import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.LocalContentColor
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import com.hzh.jetpackcomposecompositionlocal.Greeting
import com.hzh.jetpackcomposecompositionlocal.R
import com.hzh.jetpackcomposecompositionlocal.ui.theme.DN_Compose_LearnTheme

/**
 * @author huangzehui
 * @date 18/8/2025 下午10:37 周一
 * @description
 */


@Composable
fun CompositionSample2() {
    DN_Compose_LearnTheme {
        Scaffold(modifier = Modifier.fillMaxSize()) { innerPadding ->
            Column(modifier = Modifier.padding(innerPadding)) {


                Text(text = "Uses MaterialTheme's provided alpha")


                /**
                 * CompositionLocal
                 *
                 * 如需为CompositionLocal提供新值，请使用CompositionLocalProvider及其provides infix函数。
                 *
                 * CompositionLocal的current值对应于该组合部分中的某个祖先(CompositionLocalProvider)提供的最接近的值。
                 *
                 *
                 * 旧的写法
                 * CompositionLocalProvider(value = LocalContentAlpha.provides(ContentAlpha.medium)){}
                 *
                 * 新的写法如下
                 *             CompositionLocalProvider(
                 *                 value = LocalContentColor.provides(
                 *                     LocalContentColor.current.copy(
                 *                         alpha = 0.74f
                 *                     )
                 *                 )
                 *             ) {}
                 */


                CompositionLocalProvider(
                    value = LocalContentColor.provides(
                        LocalContentColor.current.copy(
                            alpha = 0.74f
                        )
                    )
                ) {
                    Text(text = "Medium value provided for LocalContentAlpha")
                    Text(text = "This Text also uses the medium value")
                }

                CompositionLocalProvider(
                    value = LocalContentColor.provides(
                        LocalContentColor.current.copy(
                            alpha = 0.38f
                        )
                    )
                ) {
                    DescendantExample()
                }



                FruitText(1)
            }
        }

    }
}


@Composable
fun DescendantExample() {
    Text(text = "This Text uses the disabled alpha now")
}

@Composable
fun FruitText(fruitSize: Int) {
    // 使用 LocalContext 来获取Android的上下文
    val resource = LocalContext.current.resources
    val fruitText = resource.getQuantityText(R.plurals.fruit_title, fruitSize)
    Text(text = "$fruitSize - $fruitText")
}